package com.example.hospitalManage.controller;

import com.example.hospitalManage.exception.NotAllowedRegException;
import com.example.hospitalManage.utils.Response;
import com.example.hospitalManage.utils.ResponseResult;
import com.example.hospitalManage.utils.StatusCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @projectName: hospitalManage
 * @package: com.example.hospitalManage.controller
 * @className: GlobalExceptionHandler
 * @author: GCT
 * @description: TODO
 * 定义一个全局处理异常类，来统一处理各种异常
 * @date: 2022/12/17 19:11
 * @version: 1.0
 */
//@RestController+@ControllerAdvice=@RestControllerAdvice
@RestControllerAdvice
public class GlobalExceptionHandler {
    //处理异常的方法1.  并确定接收的是哪种类型的异常
    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception e)
    {
        // 捕获到某个指定的异常，比如是 NotAllowedRegException 类型
        if(e instanceof NotAllowedRegException)
        {
            //处理结束后 还是要返回统一相应结果类
            return Response.createFailResp(StatusCode.NOT_ALLOWRD_REG.code,"异常:当前用户不允许注册");
        }
        else
        {
            //处理其它类型的异常 可以进入到不同的分支
            return Response.createFailResp();
        }
    }

    /*
    @ExceptionHandler(NullPointerException.class)
    public ResponseResult exceptionHandler(NullPointerException e) {

    }
    */

}
